package com.kamistoat.meimeimall.meimeimallmember.web;

import com.alibaba.fastjson.JSON;
import com.kamistoat.meimeimall.common.To.memberTo.LoginSuccessTo;
import com.kamistoat.meimeimall.common.To.memberTo.MemberReceiveAddressTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.constant.AuthConstant;
import com.kamistoat.meimeimall.common.exception.BizCodeEnum;
import com.kamistoat.meimeimall.common.exception.RRException;
import com.kamistoat.meimeimall.common.threadlocal.ThreadLocalContextHolder;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallmember.entity.MemberEntity;
import com.kamistoat.meimeimall.meimeimallmember.service.MemberReceiveAddressService;
import com.kamistoat.meimeimall.meimeimallmember.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户页面操作")
@RestController
@RequestMapping("web")
public class MemberPageController {
    protected final Logger logger = LoggerFactory.getLogger(MemberPageController.class);

    @Autowired
    MemberService memberService;
    @Autowired
    MemberReceiveAddressService memberReceiveAddressService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 已登录用户获取自身详情。
     */
    @ApiOperation("登录用户获取自身详情")
    @GetMapping("/self_member_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R selfMemberInfo() {
        LoginSuccessTo loginSuccessTo = JSON.parseObject((String) ThreadLocalContextHolder.get(AuthConstant.LOGIN_USER_THREADLOCAL_NAME), LoginSuccessTo.class);
        // 带缓查询用户详情
        MemberEntity member = memberService.getMemberEntityById(loginSuccessTo.getId());
        member.setPassword("");
        return R.ok().put("member", member);
    }


    /**
     * 已登录用户修改自身基础信息
     */
    @ApiOperation("已登录用户修改自身基础信息(不允许修改密码)")
    @PostMapping("/self_member_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R selfUpdateInfo(@RequestBody MemberEntity memberEntity) {
        LoginSuccessTo loginSuccessTo = JSON.parseObject((String) ThreadLocalContextHolder.get(AuthConstant.LOGIN_USER_THREADLOCAL_NAME), LoginSuccessTo.class);
        if(!memberEntity.getId().equals(loginSuccessTo.getId()) || !StringUtils.isEmpty(memberEntity.getPassword())) {
            throw new RRException(BizCodeEnum.FORBID_UPDATE_SELECT_MEMBER.getMessage(), BizCodeEnum.FORBID_UPDATE_SELECT_MEMBER.getCode());
        }
        // 带缓更新
        memberService.updateMemberEntity(memberEntity);
        return R.ok();
    }


    /**
     * 已登录用户修改自身密码
     */
    @ApiOperation("已登录用户修改自身密码")
    @PostMapping("/self_member_password")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R updateSecret(@RequestParam String old_password, @RequestParam String new_password) {
        LoginSuccessTo loginSuccessTo = JSON.parseObject((String) ThreadLocalContextHolder.get(AuthConstant.LOGIN_USER_THREADLOCAL_NAME), LoginSuccessTo.class);
        // 带缓更新
        memberService.updatePassword(loginSuccessTo.getId(), old_password, new_password);
        return R.ok();
    }


    /**
     * 修改密码时发送短信验证身份
     *
     * @param phoneNum
     * @return
     */
    @ApiOperation("修改密码时发送短信验证码")
    @PostMapping("/send_code_password")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R sendCode_Password(@RequestParam String phoneNum) {
        memberService.sendCodeWhenFixPassword(phoneNum);
        return R.ok();
    }

    /**
     * 检查验证码是否正确。
     *
     * @param phoneNum
     * @return
     */
    @ApiOperation("验证修改密码的验证码是否正确")
    @ResponseBody
    @PostMapping("/verify_code_password")
    @DubboAndHTTPExceptionCatch
    public R checkUpdateSecretCode(@RequestParam String phoneNum, @RequestParam String check_code) {
        memberService.verifyCodeWhenFixPassword(phoneNum, check_code);
        return R.ok();
    }


    /**
     * 已登录用户获取自身地址列表
     */
    @ApiOperation("获取用户收货地址列表")
    @GetMapping("/self_list")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R selfReceiveAddressList() {
        LoginSuccessTo loginSuccessTo = JSON.parseObject((String) ThreadLocalContextHolder.get(AuthConstant.LOGIN_USER_THREADLOCAL_NAME), LoginSuccessTo.class);
        // 带缓查询用户收获地址
        List<MemberReceiveAddressTo> memberReceiveAddressToList = memberReceiveAddressService.getMemberReceiveAddressToList(loginSuccessTo.getId());
        return R.ok().put("addressList", memberReceiveAddressToList);
    }


    /**
     * 已登录用户获取自身地址列表中的一个地址详情
     */
    @ApiOperation("已登录用户获取自身地址列表中的一个地址详情")
    @GetMapping("/self_member_receive_address_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R getAddress(@RequestParam Long addressId) {
        LoginSuccessTo loginSuccessTo = JSON.parseObject((String) ThreadLocalContextHolder.get(AuthConstant.LOGIN_USER_THREADLOCAL_NAME), LoginSuccessTo.class);
        // 带缓查询
        memberReceiveAddressService.getReceiveAddressById(loginSuccessTo.getId(), addressId);
        return R.ok();
    }


    /**
     * 已登录用户删除自身的一个收货地址。如果是默认地址需要自动补位。
     */
    @ApiOperation("已登录用户删除自身的一个收货地址。如果是默认地址需要自动补位。")
    @DeleteMapping("/self_member_receive_address_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R deleteAddress(@RequestParam Long addressId) {
        LoginSuccessTo loginSuccessTo = JSON.parseObject((String) ThreadLocalContextHolder.get(AuthConstant.LOGIN_USER_THREADLOCAL_NAME), LoginSuccessTo.class);
        // 带缓删除
        memberReceiveAddressService.deleteOneAddress(loginSuccessTo.getId(), addressId);
        return R.ok();
    }

    /**
     * 已登录用户设置自身选中地址为默认地址。原先默认地址降级
     */
    @ApiOperation("已登录用户设置自身选中地址为默认地址。原先默认地址降级")
    @PostMapping("/self_set_to_default")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R setToDefaultAddress(@RequestParam Long addressId) {
        LoginSuccessTo loginSuccessTo = JSON.parseObject((String) ThreadLocalContextHolder.get(AuthConstant.LOGIN_USER_THREADLOCAL_NAME), LoginSuccessTo.class);
        // 带缓修改
        memberReceiveAddressService.setToDefault(loginSuccessTo.getId(), addressId);
        return R.ok();
    }


    /**
     * 已登录用户 新增/修改 收货地址详情。
     * 不允许修改是否默认。
     */
    @ApiOperation("已登录用户 新增/修改 收货地址详情。")
    @PostMapping("/self_member_receive_address_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R addOrUpdateAddress(@RequestBody MemberReceiveAddressTo memberReceiveAddressTo) {
        LoginSuccessTo loginSuccessTo = JSON.parseObject((String) ThreadLocalContextHolder.get(AuthConstant.LOGIN_USER_THREADLOCAL_NAME), LoginSuccessTo.class);
        memberReceiveAddressTo.setMemberId(loginSuccessTo.getId());
        // 带缓修改
        memberReceiveAddressService.addOrUpdateAddress(memberReceiveAddressTo);
        return R.ok();
    }
}
